/**
 * 
 */
package runner;

import image.JPEGImage;
import image.bmp.BMPImage;
import image.header.JPEGHeader;

import java.io.FileOutputStream;
import java.io.IOException;

import util.Log;

import loader.ImageLoader;

/**
 * @author nquangtrung
 *
 */
public class CodecTester {
	public static final String TAG = "CodecTester";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		boolean big = true;
		boolean standard = true;
		boolean optimized = true;
		boolean quality = true;
		
		Log.turnOff(Log.DEBUG_MASK);
		Log.turnOff(Log.VERBOSE_MASK);
		if (big) {
			Log.turnOff(Log.ARRAY_MASK);
		}
		try {
			BMPImage image;
			if (big)
				image = ImageLoader.loadBMP("./image/color/Lenna.bmp");
			else
				image = ImageLoader.loadBMP("./image/simple.bmp");
			
			/*
			 * Make header, which contains all the configurations to make
			 * this jpeg image.
			 */
			JPEGHeader header = new JPEGHeader();
			header.setQuality(1.0);
			header.setHeight(image.getHeight() - image.getHeight() % 8);
			header.setWidth(image.getWidth() - image.getWidth() % 8);

			/*
			 * Make the new JPEG from the source image and configed header
			 */
			if (standard) {
				System.out.println("Encoding with Standard Huffman table...");
				header.setHuffmanTableType(JPEGHeader.DEFAULT_HUFFMAN_TABLE, null);
				JPEGImage jpg = JPEGImage.makeJPEGImage(image, header);
				
				byte[] b = jpg.getFullFile();
				FileOutputStream out = new FileOutputStream("./image/standard.jpg");
				out.write(b);
				out.close();
			}
			if (optimized) {
				System.out.println("Making Optimized Huffman table...");
				header.setHuffmanTableType(JPEGHeader.OPTIMIZED_HUFFMAN_TABLE, image);
				System.out.println("Encoding with Optimized Huffman table...");
				JPEGImage jpg = JPEGImage.makeJPEGImage(image, header);
				
				byte[] b = jpg.getFullFile();
				FileOutputStream out = new FileOutputStream("./image/optimized.jpg");
				out.write(b);
				out.close();
			}
			
			/*
			 * With standard huffman table, make images with different quality
			 */
			if (quality) {
				double[] q_ef = {0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0, 10.0};
				header.setHuffmanTableType(JPEGHeader.DEFAULT_HUFFMAN_TABLE, null);
				for (int i = 0; i < q_ef.length; i++) {
					System.out.println("Encoding with quality = " + q_ef[i] + "...");
					header.setQuality(q_ef[i]);
					JPEGImage jpg = JPEGImage.makeJPEGImage(image, header);
					
					byte[] b = jpg.getFullFile();
					FileOutputStream out = new FileOutputStream("./image/q_" + q_ef[i] + ".jpg");
					out.write(b);
					out.close();
				}
			}
			
			System.out.println("Finished!");
		} catch (IOException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}